﻿using System;
using XFControls.Utils;
using XFControls.XFPanels.XFPanelItems;
using System.Drawing;

namespace XFSenseUI.XFItems.SenseControls
{
    public class SenseControlButton : SenseControl
    {
        public bool Disabled { get; set; }
        public event EventHandler Clicked;
        public int CurveRadius;
        public string ButtonText
        {
            get
            {
                return _buttonText;
            }
            set
            {
                _buttonText = value;
                if (Style != null)
                    ItemRectangle = GenerateSize(ButtonText, Style);
            }
        }

        private string _buttonText;
        private bool _down;

        public SenseControlButton(Rectangle size)
        {
            ItemRectangle = size;
            SetupStyle();
            ItemType = XFItemType.Clickable;
        }

        public SenseControlButton(string text)
        {
            SetupStyle();
            ButtonText = text;
            ItemRectangle = GenerateSize(text, Style);

            ItemType = XFItemType.Clickable;
        }

        public override int GetHeight()
        {
            if (!string.IsNullOrEmpty(ButtonText))
                ItemRectangle = GenerateSize(ButtonText, Style);

            return ItemRectangle.Height;
        }

        public override void ResetHeight() { _down = false; }

        public override void OnMouseDown(int x, int y)
        {
            base.OnMouseDown(x, y);

            State = XFItemState.Normal;

            if (!Disabled)
            {
                State = XFItemState.Selected;
                _down = true;
            }
        }

        public override void OnMouseUp()
        {
            base.OnMouseUp();

            State = XFItemState.Normal;
            _down = false;
        }

        public override void OnBlur()
        {
            base.OnBlur();

            State = XFItemState.Normal;
            _down = false;
        }

        public override void ItemPaint(Graphics g, int x, int y)
        {
            Rectangle rec = new Rectangle(x, y, ItemRectangle.Width, ItemRectangle.Height);
            Color start = Style.SelectedBackgroundColor;
            Color end = Style.BackgroundColor;
            Color border = Style.BorderBottomColor;

            int textX = x + Style.PaddingLeft + (_down ? 2 : 0);
            int textY = y + Style.PaddingTop + (_down ? 3 : 0);

            if (!Disabled)
            {
                if (_down)
                    g.DrawRoundedRectangleBorderGradiantFill(start, end, border, rec, CurveRadius, 1);
                else
                    g.DrawRoundedRectangleBorderGradiantFill(end, start, border, rec, CurveRadius, 1);

                if (!string.IsNullOrEmpty(ButtonText))
                    using (SolidBrush b = new SolidBrush(Style.TextColor))
                        g.DrawString(ButtonText, Style.TextFont, b, textX, textY);
            }
            else
            {
                g.DrawRoundedRectangleBorder(end, border, rec, 7, 1);

                if (!string.IsNullOrEmpty(ButtonText))
                    using (SolidBrush b = new SolidBrush(start))
                        g.DrawString(ButtonText, Style.TextFont, b, textX, textY);
            }
        }

        public override XFItemClickResult GetClickResult()
        {
            State = XFItemState.Normal;
            _down = false;

            if (Disabled)
                return XFItemClickResult.Nothing;

            if (Clicked != null)
                Clicked(this, new EventArgs());

            return XFItemClickResult.Nothing;
        }

        private void SetupStyle()
        {
            Style = new XFItemStyle
                    {
                TextFont = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular),
                TextColor = Color.Black,
                DashStyleBottom = System.Drawing.Drawing2D.DashStyle.Solid,
                BorderBottomColor = Color.FromArgb(120, 120, 120),
                BackgroundColor = Color.FromArgb(240, 240, 240),
                SelectedBackgroundColor = Color.FromArgb(216, 216, 216),
                Padding = 5
            };

            Style.PropertyChanged += (o, e) =>
            {
                if (!string.IsNullOrEmpty(ButtonText))
                    ItemRectangle = GenerateSize(ButtonText, Style);
            };

            CurveRadius = 7;
        }

        private Rectangle GenerateSize(string text, XFItemStyle style)
        {
            var mes = Style.TextFont.MeasureString(text, new Rectangle(0, 0, GetWidth(), 0));
            return new Rectangle(0, 0, mes.Width + style.PaddingLeft + style.PaddingRight, mes.Height + style.PaddingTop + style.PaddingBottom);
        }
    }
}
